package cn.wolfcode.p2p.business.service.impl;

import cn.wolfcode.p2p.business.domain.Bid;
import cn.wolfcode.p2p.business.domain.ExpAccount;
import cn.wolfcode.p2p.business.domain.ExpAccountFlow;
import cn.wolfcode.p2p.business.mapper.ExpAccountFlowMapper;
import cn.wolfcode.p2p.business.service.IExpAccountFlowService;
import cn.wolfcode.p2p.util.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

@Service
public class ExpAccountFlowServiceImpl implements IExpAccountFlowService {
    @Autowired
    private ExpAccountFlowMapper expaccountFlowMapper;


    public void createBaseFlow(ExpAccount expAccount, BigDecimal amount, String note, Integer actionType) {
        ExpAccountFlow expaccountFlow = new ExpAccountFlow();
        expaccountFlow.setActionType(actionType);
        expaccountFlow.setAmount(amount);
        expaccountFlow.setNote(note);
        expaccountFlow.setUsableAmount(expAccount.getUsableAmount());
        expaccountFlow.setFreezedAmount(expAccount.getFreezedAmount());
        expaccountFlow.setActionTime(new Date());
        expaccountFlow.setExpAccountId(expAccount.getId());
        expaccountFlowMapper.insert(expaccountFlow);
    }

    @Override
    public void createFreezedSuccessFlow(String title, ExpAccount expAccount, BigDecimal amount) {
        String note = "投标["+title+"]冻结,体验金可用余额减少,体验金冻结增加["+amount+"]元";
        createBaseFlow(expAccount,amount,note,Constant.ACCOUNT_ACTIONTYPE_BID_FREEZED);
    }

    @Override
    public void createBorrowerSuccessFreezedFlow(ExpAccount expAccount, BigDecimal amount, String bidRequestTitle) {
        String note = "投标["+ bidRequestTitle + "]标成功,体验金冻结资金减少[" + amount +"]元";
        createBaseFlow(expAccount,amount,note,Constant.ACCOUNT_ACTIONTYPE_BID_SUCCESSFUL);
    }

    @Override
    public void createBidFailFlow(ExpAccount expAccount, Bid bid) {
        String note = "投标[" + bid.getBidRequestTitle() + "]失败,体验金冻结减少,可用余额增加[" + bid.getAvailableAmount()+"]元";
        createBaseFlow(expAccount,bid.getAvailableAmount(),note,Constant.ACCOUNT_ACTIONTYPE_BID_UNFREEZED);
    }

    @Override
    public void createReturnExperienceGoldPrincipal(ExpAccount expAccount, BigDecimal amount, String bidRequestTitle) {
        String note = "[" + bidRequestTitle + "]标退回本金,体验金可用余额增加[" + amount +"]元";
        createBaseFlow(expAccount,amount,note,Constant.Return_Experience_Gold_Principal);
    }

    @Override
    public void createRecyclingExperienceFlow(ExpAccount expAccount, BigDecimal amount, String note, Integer actionType) {
        String text = note + "[" + amount + "]元";
        createBaseFlow(expAccount,amount,note,actionType);
    }


    public void createReceiveExperienceFlow(ExpAccount expAccount, BigDecimal amount, String note, Integer actionType){
        String text = note + "[" + amount + "]元";
        createBaseFlow(expAccount,amount,note,actionType);

    }
}
